//
//  BMBicycleAnnotationCalloutView.m
//  BicycleMap
//
//  Created by vedon on 5/19/16.
//  Copyright © 2016 Qianqian. All rights reserved.
//
#define kAvatarSize CGSizeMake(30, 30)
#define KLabelPadding 10

#import "BMBicycleAnnotationCalloutView.h"
#import "PureLayout.h"
#import "UIImageView+WebCache.h"

@interface BMBicycleAnnotationCalloutView ()
@property (strong,nonatomic) UIImageView *avatar;
@property (strong,nonatomic) UILabel *usernameLabel;
@property (strong,nonatomic) UILabel *mobileLabel;
@property (strong,nonatomic) UIButton *navButton;
@end

@implementation BMBicycleAnnotationCalloutView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self addSubview:self.avatar];
        [self addSubview:self.usernameLabel];
        [self addSubview:self.mobileLabel];
        [self addSubview:self.navButton];
        
        [self.avatar autoAlignAxis:ALAxisVertical toSameAxisOfView:self];
        [self.avatar autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self withOffset:KLabelPadding];
        [self.avatar autoSetDimensionsToSize:kAvatarSize];
        
        [self.navButton autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self withOffset:-KLabelPadding];
        [self.navButton autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self withOffset:KLabelPadding];
        [self.navButton autoSetDimensionsToSize:CGSizeMake(40, 25)];
        
        
        [self.usernameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.avatar withOffset:KLabelPadding];
        [self.usernameLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self withOffset:KLabelPadding];
        [self.usernameLabel autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self withOffset: -KLabelPadding];
        [self.usernameLabel autoSetDimension:ALDimensionHeight toSize:20];
        
        
        [self.mobileLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.usernameLabel withOffset:KLabelPadding/2];
        [self.mobileLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self withOffset:KLabelPadding];
        [self.mobileLabel autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self withOffset:-KLabelPadding];
        [self.mobileLabel autoSetDimension:ALDimensionHeight toSize:20];
        
        
        
    }
    return self;
}

+ (CGFloat)mimimumHeight
{
    return 100;
}

- (void)configureWithData:(BMPointAnnotation *)data
{
    [self.avatar sd_setImageWithURL:[NSURL URLWithString:data.userInfo.avatarUrl] placeholderImage:[UIImage imageNamed:@"annotationViewDefaultImage"]];
    self.usernameLabel.text = [NSString stringWithFormat:@"姓名：%@",data.userInfo.username];
    self.mobileLabel.text = [NSString stringWithFormat:@"手机：%@",data.userInfo.mobile];
}

- (void)didTapNavButton
{
    if ([self.delegate respondsToSelector:@selector(didTapNavButton)])
    {
        [self.delegate didTapNavButton];
    }
}


#pragma mark - Getter & Setter

- (UIImageView *)avatar
{
    
    if (!_avatar)
    {
        _avatar = [[UIImageView alloc] initForAutoLayout];
        _avatar.layer.cornerRadius = kAvatarSize.width/ 2;
        _avatar.layer.masksToBounds = YES;
        _avatar.contentMode = UIViewContentModeScaleAspectFill;
        
        
    }
    return _avatar;
}

- (UILabel *)usernameLabel
{
    if (!_usernameLabel)
    {
        _usernameLabel = [[UILabel alloc] initForAutoLayout];
        _usernameLabel.textColor = [UIColor whiteColor];
        _usernameLabel.font = [UIFont systemFontOfSize:14];
    }
    return _usernameLabel;
}

- (UILabel *)mobileLabel
{
    if (!_mobileLabel)
    {
        _mobileLabel = [[UILabel alloc] initForAutoLayout];
        _mobileLabel.textColor = [UIColor whiteColor];
        _mobileLabel.font = [UIFont systemFontOfSize:14];
    }
    return _mobileLabel;
}

- (UIButton *)navButton
{
    if (!_navButton)
    {
        _navButton = [[UIButton alloc] initForAutoLayout];
        [_navButton setTitle:@"导航" forState:UIControlStateNormal];
        [_navButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _navButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_navButton addTarget:self action:@selector(didTapNavButton) forControlEvents:UIControlEventTouchUpInside];
    }
    return _navButton;
}


@end
